package org.lgiavedoni.android.franq.util.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class CollectionUtils {

	public static List<?> convert(Object[] objs) {
		ArrayList l = new ArrayList();
		for (int i = 0; i < objs.length; i++) {
			l.add(objs[i]);
		}
		return l;
	}

	public static List<?> toList(Object... objs) {
		ArrayList l = new ArrayList();
		for (int i = 0; i < objs.length; i++) {
			l.add(objs[i]);
		}
		return l;
	}

	public static String[] removeElement(String[] objs, int element) {
		if (objs == null)
			return null;
		if (objs.length == 1)
			return new String[0];

		String[] result = new String[objs.length - 1];
		int j = 0;
		for (int i = 0; i < objs.length; i++) {

			if (i != element) {
				result[j] = objs[i];
				j++;
			}
		}
		return result;
	}

	public static String toString(Collection<?> set, String concat) {
		StringBuffer buff = new StringBuffer();
		for (Object obj : set) {
			buff.append(obj.toString());
			buff.append(concat);
		}

		String result = buff.toString();
		return StringUtils.removeEnd(result, concat);
	}

	public static String toString(Map<?, ?> set, String concatKV, String concatLines) {
		StringBuffer buff = new StringBuffer();
		for (Object obj : set.keySet()) {
			buff.append(obj.toString());
			buff.append(concatKV);
			buff.append(set.get(obj));
			buff.append(concatLines);
		}

		String result = buff.toString();
		return StringUtils.removeEnd(result, concatLines);
	}

	public static String toString(Iterator<?> set, String concat) {
		List<String> list = new ArrayList<String>();
		while (set.hasNext()) {
			Object o = set.next();
			if (o != null)
				list.add(o.toString());
			else
				list.add(null);
		}
		return toString(list, concat);
	}

	public static boolean cointansAny(Collection<?> a, Collection<?> b) {
		if (a == null || b == null)
			return false;
		for (Object o : a) {
			if (b.contains(o))
				return true;
		}
		return false;
	}

	public static boolean cointansNull(Collection<?> c) {
		if (c == null)
			return true;

		for (Object o : c) {
			if (o == null)
				return true;
		}
		return false;
	}

	public static String toString(Object[] set, String concat) {
		StringBuffer result = new StringBuffer();
		for (Object obj : set) {
			result.append(obj.toString());
			result.append(concat);
		}
		return result.toString();
	}

	public static boolean isEmpty(Collection<?> c) {
		return c == null || c.size() == 0 ? true : false;
	}

	public static boolean isNotEmpty(Collection<?> c) {
		return !isEmpty(c);
	}

	public static List getLast(List l, int size) {
		if ((l.size() - size) >= 0)
			return l.subList(l.size() - size, l.size());
		return l;
	}

	public static List reverse(List l) {
		List<Object> l2 = new ArrayList();
		for (int i = l.size() - 1; i >= 0; i--) {
			l2.add(l.get(i));
		}
		return l2;
	}

	public static boolean cointansNull(Object[] objects) {
		if (objects == null)
			return true;
		for (int i = 0; i < objects.length; i++) {
			if (objects[i] == null)
				return true;
		}
		return false;
	}

	public static void setAll(Collection data, Collection destination) {
		for (Object o : data) {
			destination.add(o);
		}
	}
}
